#include <gtest/gtest.h>

#include "SkipList.h"

class SkipListPerformanceTest : public ::testing::Test {
protected:
    const size_t MAX_ELEMENTS = 1'000'000;
    const size_t MAX_LEVEL = 18;
    skip_list::SkipList<int, std::string> skip_list{MAX_LEVEL};
};

// 单线程插入性能测试
TEST_F(SkipListPerformanceTest, SingleThreadInsert) {
    for (size_t i = 0; i < MAX_ELEMENTS; ++i) {
        skip_list.insert_element(i, "value_" + std::to_string(i));
    }

    EXPECT_EQ(skip_list.size(), MAX_ELEMENTS);
}

// 多线程插入性能测试
TEST_F(SkipListPerformanceTest, MultiThreadInsert) {
    const int NUM_THREADS = 8;
    std::vector<std::thread> threads;

    // 创建多个线程并发插入
    for (int t = 0; t < NUM_THREADS; ++t) {
        threads.emplace_back([this, t, NUM_THREADS]() {
            size_t start_idx = t * (MAX_ELEMENTS / NUM_THREADS);
            size_t end_idx = (t + 1) * (MAX_ELEMENTS / NUM_THREADS);

            for (size_t i = start_idx; i < end_idx; ++i) {
                skip_list.insert_element(i, "value_" + std::to_string(i));
            }
        });
    }

    // 等待所有线程完成
    for (auto& thread: threads) {
        thread.join();
    }
    EXPECT_EQ(skip_list.size(), MAX_ELEMENTS);
}

// 查询性能测试
TEST_F(SkipListPerformanceTest, SearchPerformance) {
    // 先插入数据
    for (size_t i = 0; i < MAX_ELEMENTS; ++i) {
        skip_list.insert_element(i, "value_" + std::to_string(i));
    }

    auto start = std::chrono::high_resolution_clock::now();

    // 随机查询
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, MAX_ELEMENTS - 1);

    size_t successful_searches = 0;
    for (size_t i = 0; i < MAX_ELEMENTS; ++i) {
        int key = dis(gen);
        std::string value;
        if (skip_list.search_element(key, &value)) {
            successful_searches++;
        }
    }
}

// 删除性能测试
TEST_F(SkipListPerformanceTest, DeletePerformance) {
    // 先插入数据
    for (size_t i = 0; i < MAX_ELEMENTS; ++i) {
        skip_list.insert_element(i, "value_" + std::to_string(i));
    }

    auto start = std::chrono::high_resolution_clock::now();

    // 删除所有元素
    for (size_t i = 0; i < MAX_ELEMENTS; ++i) {
        skip_list.delete_element(i);
    }
    EXPECT_EQ(skip_list.size(), 0);
}

// 并发读写性能测试
TEST_F(SkipListPerformanceTest, ConcurrentReadWrite) {
    const int NUM_THREADS = 8;
    std::vector<std::thread> write_threads;
    std::vector<std::thread> read_threads;

    // 并发写入线程
    for (int t = 0; t < NUM_THREADS; ++t) {
        write_threads.emplace_back([this, t, NUM_THREADS]() {
            size_t start_idx = t * (MAX_ELEMENTS / NUM_THREADS);
            size_t end_idx = (t + 1) * (MAX_ELEMENTS / NUM_THREADS);

            for (size_t i = start_idx; i < end_idx; ++i) {
                skip_list.insert_element(i, "value_" + std::to_string(i));
            }
        });
    }

    // 并发读取线程
    for (int t = 0; t < NUM_THREADS; ++t) {
        read_threads.emplace_back([this, t, NUM_THREADS]() {
            size_t start_idx = t * (MAX_ELEMENTS / NUM_THREADS);
            size_t end_idx = (t + 1) * (MAX_ELEMENTS / NUM_THREADS);

            for (size_t i = start_idx; i < end_idx; ++i) {
                std::string value;
                skip_list.search_element(i, &value);
            }
        });
    }

    // 等待所有线程完成
    for (auto& thread: write_threads) {
        thread.join();
    }
    for (auto& thread: read_threads) {
        thread.join();
    }
}
